package com.huiquan.answer.controller;

import com.huiquan.answer.service.AnswerService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lichao email:lichao@witspring.com
 * @Description:
 * @Date 2018/2/7 14:47
 * @since Ver V
 */
@Controller
@RequestMapping("/answer")
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    @SuppressWarnings("unchecked")
    @RequestMapping("/version_list.htm")
    public ModelAndView versionList(HttpServletRequest req) {
        String tabid = req.getParameter("tabid");
        Map<String, Object> pageParam;
        if (tabid != null && !tabid.isEmpty()) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
        }
        // 获取页面参数，如果request中有值则修改session，否则从session中拿值
        String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
        return answerService.versionList(start);

    }

    @SuppressWarnings("unchecked")
    @RequestMapping("/answer_list.htm")
    public ModelAndView answerList(HttpServletRequest req) {
        String tabid = req.getParameter("tabid");
        Map<String, Object> pageParam;
        if (tabid != null && !tabid.isEmpty()) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            if (tabid.endsWith("1")) {
                pageParam.put("status", 1);
            } else if (tabid.endsWith("2")) {
                pageParam.put("status", 2);
            }
            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
        }
        // 获取页面参数，如果request中有值则修改session，否则从session中拿值
        String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
        String status = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "status");
        String version = req.getParameter("version");
        return answerService.answerList(start, version, status);
    }

    @ResponseBody
    @RequestMapping("/answer_check_flag.do")
    public ReturnData answerCheckFlag(HttpServletRequest req) {
        String id = RequestParamUtil.getTrimString(req, "id");
        String flag = RequestParamUtil.getTrimString(req, "flag");
        return answerService.answerCheckFlag(id, flag);
    }
}
